// -----------------------------------------------------------------------
//  <copyright file="ApiResultExtensions.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2024 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://www.dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2024-05-10 14:05</last-date>
// -----------------------------------------------------------------------

using DaprPlus.Domain;


namespace DaprPlus.AspNetCore.Extensions;

public static class ApiResultExtensions
{
    /// <summary>
    /// 将API错误转换为ActionResult
    /// </summary>
    public static ActionResult ToActionResult(this ApiResult result)
    {
        return result.ResultType switch
        {
            ApiResultType.Info => new ObjectResult(result) { StatusCode = StatusCodes.Status202Accepted },
            ApiResultType.Success => new OkObjectResult(result),
            ApiResultType.NotFound => new NotFoundResult(),
            ApiResultType.UnAuth => new UnauthorizedResult(),
            ApiResultType.Forbidden => new ForbidResult(),
            ApiResultType.Locked => new BadRequestObjectResult(result) { StatusCode = StatusCodes.Status423Locked },
            ApiResultType.Error => new ObjectResult(result) { StatusCode = StatusCodes.Status500InternalServerError },
            _ => new ObjectResult(result) { StatusCode = StatusCodes.Status500InternalServerError }
        };
    }
}
